// components/pagination/pagination.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    current: { //当前页码
      type: Number,
      value: 1
    },
    pages: { //总页数
      type: Number,
      value: 0
    },
    total: { //当前条数
      type: Number,
      value: 0
    },
    // preData: {
    //   type: Object,
    //   value: {}
    // },
    // nextData: {
    //   type: Object,
    //   value: {
    //     label: 2,
    //     active:true
    //   }
    // },
    // list: {
    //   type: Array,
    //   value: []
    // }
  },
  observers: {
    'pages' (data){
      console.log('pages变化了',data,this.data.pages)
      this.data.current = 1
      this.init()
    }
  },
  created(){
    
  },
  attached(){
    this.init()
  },
  /**
   * 组件的初始数据
   */
  data: {
    
    listLength: 5,
    preData:{
    },
    nextData: {
      label: 2,
      active:true
    },
    list: [
     
    ],
    
  },
 
  /**
   * 组件的方法列表
   */
  lifetimes: {

  },
  methods: {
    handlePageClick(e){
      const item = e.currentTarget.dataset.item
      const index = e.currentTarget.dataset.index
      console.log(item)
      let current = item.label
      if(this.data.current == item.label || item.label > this.data.pages || item.label < 1){
        return 
      }
      else {
        if(this.data.pages < this.data.listLength){
          this.data.list.forEach(item=>{
            item.active = false
          })
          this.data.list[index].active = true
        }
        else {
          // 前后都有2个
          if(item.label + 2 <= this.data.pages && item.label - 2 > 0){
            this.data.list = [
              {
                label: item.label - 2,
                active: false
              },
              {
                label: item.label - 1,
                active: false
              },
              {
                label: item.label,
                active: true
              },
              {
                label: item.label + 1,
                active: false
              },
              {
                label: item.label + 2,
                active: false
              }
            ]
          }
          else {
            

            // 仅后面不足
            if(item.label + 2 > this.data.pages && item.label - 2 > 0){
              let len = item.label + 2 - this.data.pages 
              let _list = [
                {
                  label: item.label,
                  active: true
                },
              ]
              for(let i = 0;i< 2 + len;i++){
                _list.unshift(
                  {
                    label: item.label - (i + 1),
                    active: false
                  }
                )
                
              }
              for(let i = 0;i< 2 - len;i++){
                _list.push(
                  {
                    label: item.label + i + 1,
                    active: false
                  }
                )
              }
             
              this.data.list = _list
            }
            // // 仅前面不足
            else if(item.label + 3 < this.data.pages && item.label - 2 <= 0) {
              let len = 1 - (item.label - 2) 
              let _list = [
                {
                  label: item.label,
                  active: true
                },
              ]
              for(let i = 0;i< 2 - len;i++){
                _list.unshift(
                  {
                    label: item.label - (i + 1),
                    active: false
                  }
                )
                
              }
              for(let i = 0;i< 2 + len;i++){
                _list.push(
                  {
                    label: item.label + i + 1,
                    active: false
                  }
                )
              }
              this.data.list = _list
              
            }
            else {
              console.log('前后都不够')
              this.data.list.forEach(item=>{
                item.active = false
              })
              this.data.list[index].active = true
            }
          }
        }
        
        // this.setData({
        //   current,
        //   list: this.data.list
        // })
        this.updpateData({
          current,
          list: this.data.list
        })
        console.log(current,this.data.list)
      }
    },
    handleLastClick(){
      let list = [
        {
          label: this.data.pages,
          active: true
        },
      ]
      // 如果页数小于5
      if(this.data.pages < this.data.listLength){
        this.data.list.forEach(item=>{
          item.active = false
        })
        this.data.list[this.data.pages - 1].active = true
        list = [...this.data.list]
      }
      else {
        for(let i = this.data.pages;i>2;i--){
          list.unshift(
            {
              label: i - 1,
              active: false
            }
          )
        }
      }
      // this.setData({
      //   current: this.data.pages ,
      //   list
      // })
      this.updpateData({
        current: this.data.pages,
        list
      })
    },
    handleFirstClick(){
      let list = [
        {
          label: 1,
          active: true
        },
      ]
      // 如果页数小于5
      if(this.data.pages <= this.data.listLength){
        this.data.list.forEach(item=>{
          item.active = false
        })
        this.data.list[0].active = true
        list = [...this.data.list]
      }
      else {
        for(let i = 2;i < this.data.pages;i++){
          list.push(
            {
              label: i,
              active: false
            }
          )
        }
      }
      // this.setData({
      //   current: 1 ,
      //   list
      // })
      this.updpateData({
        current: 1 ,
        list
      })
    },
    updpateData(data){
      this.setData({
        current: data.current || this.data.current ,
        list: data.list  || this.data.list,
        nextData: {
          label: data.current + 1,
          active: true
        },
        preData: {
          label: data.current - 1,
          active: true
        }
      })
      this.triggerEvent('pageChange',this.data.current)
    },
    init(){
      const length = this.data.pages > this.data.listLength ? this.data.listLength : this.data.pages
      let list = []
      let current = this.data.current
      for(let i = 1;i<current+length;i++){
        let active = false
        if(i == current){
          active = true
        }
        list.push({
          label: i,
          active
        })
      }
      // this.setData({
      //   list
      // })
      // this.updpateData({
      //   current: 1 ,
      //   list
      // })
      this.setData({
        current: 1 ,
        list
      })
    }
  }
})